package com.efast.dmom.mgmt.controller.les.pda;

import com.efast.cafe.framework.bean.ResultBean;
import com.efast.cafe.framework.exception.ServiceException;
import com.efast.cafe.framework.util.SpringWebUtils;
import com.efast.dmom.mgmt.bean.les.PdaTmMmDeliverySheetPartBean;
import com.efast.dmom.mgmt.entity.les.TmMmDeliverySheet;
import com.efast.dmom.mgmt.entity.les.TmMmDeliverySheetOp;
import com.efast.dmom.mgmt.entity.les.TmMmDeliverySheetPart;
import com.efast.dmom.mgmt.entity.les.TwDocOrderDetails;
import com.efast.dmom.mgmt.service.les.TmMmDeliverySheetOpService;
import com.efast.dmom.mgmt.service.les.TmMmDeliverySheetPartService;
import com.efast.dmom.mgmt.service.les.TmMmDeliverySheetService;
import com.efast.dmom.mgmt.service.les.TwDocOrderDetailsService;
import com.efast.dmom.mgmt.utils.Assist;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.Date;
import java.util.List;

/**
 *
 * 物料上线 -- Material line 涉及数据表：看板上线单头表 tm_mm_delivery_sheet 看板上线单明细tm_mm_delivery_sheet_part
 * 
 * @author ChenLiPeng
 */
@Controller
@RequestMapping(value = "/pdaMaterialLine")
@Api(description = "pda->拉动->7.5.4.2看板上线")
public class PdaMaterialLineController {

	/**
	 * 看板上线单头表 tm_mm_delivery_sheet
	 */
	private final TmMmDeliverySheetService tmMmDeliverySheetService;
	/**
	 * 看板上线单明细tm_mm_delivery_sheet_part
	 */
	private final TmMmDeliverySheetPartService tmMmDeliverySheetPartService;
	/**
	 * 看板上线单历史记录 tm_mm_delivery_sheet_op
	 */
	private final TmMmDeliverySheetOpService tmMmDeliverySheetOpService;
	/**
	 *
	 * 出库订单表 detail tw_doc_order_details
	 */
	private final TwDocOrderDetailsService twDocOrderDetailsService;

	@Autowired
	public PdaMaterialLineController(TmMmDeliverySheetService tmMmDeliverySheetService,
			TmMmDeliverySheetPartService tmMmDeliverySheetPartService,
			TmMmDeliverySheetOpService tmMmDeliverySheetOpService, TwDocOrderDetailsService twDocOrderDetailsService) {
		this.tmMmDeliverySheetService = tmMmDeliverySheetService;
		this.tmMmDeliverySheetPartService = tmMmDeliverySheetPartService;
		this.tmMmDeliverySheetOpService = tmMmDeliverySheetOpService;
		this.twDocOrderDetailsService = twDocOrderDetailsService;
	}

	/**
	 *
	 * 对物料上线提供【上线】 点击“上线”这个按钮，即代表“线边已接收”，但是需要判断该条记录是否已经 出库
	 *
	 * @param reqTmMmDeliverySheetParts
	 *            {"sheetNo":"XXX","lineReceiveQty":XXX,"lineReceiveBoxnum":xxx,"tmMmDeliverySheetPartId":xx}
	 * @return ResultBean
	 */
	@ApiOperation(value = "对物料上线提供【上线】", notes = "该接口前台需要传递的数据格式：  {\"sheetNo\":\"XXX\",\"lineReceiveQty\":XXX,\"lineReceiveBoxnum\":xxx,\"tmMmDeliverySheetPartId\":xx}")
	@RequestMapping(value = "/line", method = RequestMethod.POST)
	public @ResponseBody
	ResultBean line(@RequestBody List<TmMmDeliverySheetPart> reqTmMmDeliverySheetParts) {
		ResultBean result = new ResultBean(true, "'");
		// 【1】 : 通过sheet_no查询出唯一的一条记录
		List<TmMmDeliverySheet> listHeader = tmMmDeliverySheetService.selectTmMmDeliverySheet(new Assist(
				Assist.andEq("tm_mm_delivery_sheet.sheet_no", reqTmMmDeliverySheetParts.get(0).getSheetNo())));
		if (listHeader.size() == 0) {
			throw new ServiceException(reqTmMmDeliverySheetParts.get(0).getSheetNo() + "查询不到看板上线指示单");
		}

		boolean ifSendSuccess = false;
		for (TmMmDeliverySheetPart reqTmMmDeliverySheetPart : reqTmMmDeliverySheetParts) {
			// 【2】：根据tm_mm_delivery_sheet_part.tm_mm_delivery_sheet_part_id =
			// tw_doc_order_details.cust_sheet_line_id
			// tm_mm_delivery_sheet_part.cust_sheet_id = tm_mm_delivery_sheet.sheet_no
			// 次记录关联依据是在“组单”（TtMmManualMriServiceImpl）的时候，关联插入的。
			List<TwDocOrderDetails> listTwDocOrderDetails = twDocOrderDetailsService.selectTwDocOrderDetails(
					new Assist(Assist.andEq("tw_doc_order_details.cust_sheet_id", listHeader.get(0).getSheetNo()),
							Assist.andEq("tw_doc_order_details.cust_sheet_line_id",
									reqTmMmDeliverySheetPart.getTmMmDeliverySheetPartId())));

			// 状态（发运任务未产生0、发运任务产生1、部分发运2、发运完成3）
			TmMmDeliverySheetPart tmMmDeliverySheetPart1 = tmMmDeliverySheetPartService.selectTmMmDeliverySheetPartById(
					listTwDocOrderDetails.get(0).getCustSheetLineId());
			if ("3".equals(listTwDocOrderDetails.get(0).getItemLoadStatus())) {
				// 需求单状态（0新单 1已发布 2线边已接收-未出库 3线边已接收-已出库 4线边已拒收）
				tmMmDeliverySheetPart1.setItemStatus("3");
			} else {
				// 说明出库单有明细没有出库的
				ifSendSuccess = true;
				tmMmDeliverySheetPart1.setItemStatus("2");
			}
			// 除了修改状态之外，还需要修改 对应实际的 线旁已收货数量 和 线旁已收货箱数
			tmMmDeliverySheetPart1.setLineReceiveQty(reqTmMmDeliverySheetPart.getLineReceiveQty());
			tmMmDeliverySheetPart1.setLineReceiveBoxnum(reqTmMmDeliverySheetPart.getLineReceiveBoxnum());
			tmMmDeliverySheetPartService.updateNonEmptyTmMmDeliverySheetPartById(tmMmDeliverySheetPart1);
		}

		// 【3】：更新 看板上线单头表 tm_mm_delivery_sheet
		TmMmDeliverySheet tmMmDeliverySheet = new TmMmDeliverySheet();
		tmMmDeliverySheet.setTmMmDeliverySheetId(listHeader.get(0).getTmMmDeliverySheetId());
		if (ifSendSuccess) {
			// 需求单状态（0新单 1已发布 2线边已接收-未出库 3线边已接收-已出库 4线边已拒收）
			tmMmDeliverySheet.setSheetStatus("2");
		} else {
			// 需求单状态（0新单 1已发布 2线边已接收-未出库 3线边已接收-已出库 4线边已拒收）
			tmMmDeliverySheet.setSheetStatus("3");
		}
		tmMmDeliverySheetService.updateNonEmptyTmMmDeliverySheetById(tmMmDeliverySheet);

		// 【5】 看板上线单历史记录 tm_mm_delivery_sheet_op
		TmMmDeliverySheetOp tmMmDeliverySheetOp = new TmMmDeliverySheetOp();
		tmMmDeliverySheetOp.setTmMmDeliverySheetId(listHeader.get(0).getTmMmDeliverySheetId());
		tmMmDeliverySheetOp.setCreateTime(new Date());
		tmMmDeliverySheetOp.setCompanyCode(SpringWebUtils.getRequestCompany());
		tmMmDeliverySheetOp.setCreateUser(SpringWebUtils.getRequestUser());
		if (ifSendSuccess) {
			System.out.println();
			// 操作备注
			tmMmDeliverySheetOp.setOperateNotes("线边已接收-未出库");
			// 需求单状态（0新单 1已发布 2线边已接收-未出库 3线边已接收-已出库 4线边已拒收）
			tmMmDeliverySheetOp.setCommonSheetOpType("2");
		} else {
			// 操作备注
			tmMmDeliverySheetOp.setOperateNotes("线边已接收-已出库");
			// 需求单状态（0新单 1已发布 2线边已接收-未出库 3线边已接收-已出库 4线边已拒收）
			tmMmDeliverySheetOp.setCommonSheetOpType("3");
		}
		tmMmDeliverySheetOp.setOperateUsername(SpringWebUtils.getRequestUser());
		tmMmDeliverySheetOp.setMarkStatus(1);
		tmMmDeliverySheetOpService.insertNonEmptyTmMmDeliverySheetOp(tmMmDeliverySheetOp);

		result.setMessage("上线成功");
		return result;
	}

	@ApiOperation(value = "【根据sheetno单号查询记录】", notes = "该接口前台需要传递的数据格式：{\"sheetNo\":\"XXX\"}")
	@ResponseBody
	@RequestMapping(value = "/query", method = RequestMethod.POST)
	public ResultBean queryHis(@RequestBody TmMmDeliverySheet tmMmDeliverySheet) {
		ResultBean result = new ResultBean(true, "");
		List<PdaTmMmDeliverySheetPartBean> tmMmDeliverySheetParts = tmMmDeliverySheetPartService.queryTmMmDeliverySheetPartBySheetNo(
				tmMmDeliverySheet.getSheetNo());
		if(tmMmDeliverySheetParts.size()==0) {
			throw new ServiceException("此看板单号没有查询到数据");
		}
		result.setObject(tmMmDeliverySheetParts);
		return result;
	}

}
